// 自定义全局工具类 by tianmh
import COMMON from '@/common/common.js'

const preventRepeatEvent = {}
export default {
    cleanParams: cleanParams,
    isNull: isNull,
    getPageUrl: getPageUrl,
    getCurrentDate: getCurrentDate,
    unique: unique,
    goBack: goBack,
    // 缓存类
    getToken: getToken,
    setToken: setToken,
    getAccount: getAccount,
    setAccount: setAccount,
    setPassword: setPassword,
    getPassword: getPassword,
    toast: toast,
    toastAndBack: toastAndBack,
    callBuzzer: callBuzzer,
    requestRest: requestRest,
    preventRepeat: preventRepeat,
    requestRestget: requestRestget,
    setName: setName,
    getuserName: getuserName,
    setVersion: setVersion,
    getversionCode: getversionCode,
    setOrgCode: setOrgCode,
    getOrgCode: getOrgCode,
    setOrgName: setOrgName,
    getOrgName: getOrgName,
    toastLong: toastLong,
    throttle: throttle,
	requestRestgetnoparam:requestRestgetnoparam,
	getitemlist:getitemlist,
	setitemlist:setitemlist,
	settaskCode,
	gettaskCode,
	setInventorysupplies,
	getInventorysupplies,
	removeInventorysupplies,
	setMaterialcoding,
	removeMaterialcoding,
	getMaterialcoding,
	setBoxinformationbinding,
	removeBoxinformationbinding,
	getBoxinformationbinding,
	setInventorycounts,
	removeInventorycounts,
	getInventorycounts,
	setloceq,
	getloceq,
	setunlineMaterialcoding,
	removeunlineMaterialcoding,
	getunlineMaterialcoding,
	setunlineBoxinformationbinding,
	removeunlineBoxinformationbinding,
	getunlineBoxinformationbinding,
	setunlineaddress,
	getunlineaddress,
	setunlinelogin,
	getunlinelogin,
	settaskId,
	gettaskId
}


// 离线地址
function setunlineaddress(unlineaddress){
	uni.setStorage({
	    key: 'unlineaddress',
	    data: unlineaddress
	})
}
function removeunlineaddress(){
	return uni.removeStorageSync("unlineaddress");
}
function getunlineaddress(){
	return uni.getStorageSync("unlineaddress");
}

// 离线鉴权
function setunlinelogin(unlinelogin){
	uni.setStorage({
	    key: 'unlinelogin',
	    data: unlinelogin
	})
}
function removeunlinelogin(){
	return uni.removeStorageSync("unlinelogin");
}
function getunlinelogin(){
	return uni.getStorageSync("unlinelogin");
}

// 离线箱组绑定信息
function setunlineBoxinformationbinding(unlineBoxinformationbinding){
	uni.setStorage({
	    key: 'unlineBoxinformationbinding',
	    data: unlineBoxinformationbinding
	})
}
function removeunlineBoxinformationbinding(){
	return uni.removeStorageSync("unlineBoxinformationbinding");
}
function getunlineBoxinformationbinding(){
	return uni.getStorageSync("unlineBoxinformationbinding");
}



// 离线物资设备符码
function setunlineMaterialcoding(unlineMaterialcoding){
	uni.setStorage({
	    key: 'unlineMaterialcoding',
	    data: unlineMaterialcoding
	})
	console.info("设置unlineMaterialcoding成功 " + unlineMaterialcoding);
}
function removeunlineMaterialcoding(){
	return uni.removeStorageSync("unlineMaterialcoding");
}
function getunlineMaterialcoding(){
	return uni.getStorageSync("unlineMaterialcoding");
}


// 当前箱信息绑定
function setBoxinformationbinding(Boxinformationbinding){
	uni.setStorage({
	    key: 'Boxinformationbinding',
	    data: Boxinformationbinding
	})
	console.info("设置Boxinformationbinding成功 " + Boxinformationbinding);
}
function removeBoxinformationbinding(){
	return uni.removeStorageSync("Boxinformationbinding");
}
function getBoxinformationbinding(){
	return uni.getStorageSync("Boxinformationbinding");
}



// 当前物资赋码
function setMaterialcoding(Materialcoding){
	uni.setStorage({
	    key: 'Materialcoding',
	    data: Materialcoding
	})
	console.info("设置Materialcoding成功 " + Materialcoding);
}
function removeMaterialcoding(){
	return uni.removeStorageSync("Materialcoding");
}
function getMaterialcoding(){
	return uni.getStorageSync("Materialcoding");
}



// 当前物资赋码
function setloceq(loceq){
	uni.setStorage({
	    key: 'loceq',
	    data: loceq
	})
	console.info("设置loceq成功 " + loceq);
}
function getloceq(){
	return uni.getStorageSync("loceq");
}


// 当前物资清点列表
function setInventorysupplies(Inventorysupplies){
	uni.setStorage({
	    key: 'Inventorysupplies',
	    data: Inventorysupplies
	})
	console.info("设置Inventorysupplies成功 " + Inventorysupplies);
}
function removeInventorysupplies(){
	return uni.removeStorageSync("Inventorysupplies");
}
// 当前物资清点列表
function getInventorysupplies(){
	return uni.getStorageSync("Inventorysupplies");
}


// 当前物资清点列表
function setInventorycounts(Inventorycounts){
	uni.setStorage({
	    key: 'Inventorycounts',
	    data: Inventorycounts
	})
	console.info("设置Inventorycounts成功 " + Inventorycounts);
}
function removeInventorycounts(){
	return uni.removeStorageSync("Inventorycounts");
}
// 当前物资清点列表
function getInventorycounts(){
	return uni.getStorageSync("Inventorycounts");
}

// 当前选择任务taskId
function settaskId(taskId){
	uni.setStorage({
	    key: 'taskId',
	    data: taskId
	})
	console.info("设置taskId成功 " + taskId);
}
// 当前选择任务taskId
function gettaskId(){
	return uni.getStorageSync("taskId");
}


// 当前选择任务taskCode
function settaskCode(taskCode){
	uni.setStorage({
	    key: 'taskCode',
	    data: taskCode
	})
	console.info("设置taskCode成功 " + taskCode);
}
// 当前选择任务taskCode
function gettaskCode(){
	return uni.getStorageSync("taskCode");
}


//按钮节流
export function throttle(fn, gapTime) {
    if (gapTime == null || gapTime == undefined) {
        gapTime = 1500
    }
    let _lastTime = null
    // 返回新的函数
    return function() {
        let _nowTime = +new Date()
        if (_nowTime - _lastTime > gapTime || !_lastTime) {
            fn.apply(this, arguments) //将this和参数传给原函数
            _lastTime = _nowTime

        }
    }
}

function callBuzzer() {
    // #ifdef APP-PLUS
    uni.requireNativePlugin("BuzzerModule").call()
    // #endif
}

function toast(content) {
    uni.showToast({
        title: content,
        icon: 'none',
        duration: 2000
    });
}

function toastLong(content) {
    uni.showToast({
        title: content,
        icon: 'none',
        duration: 6000
    });
}




function setitemlist(itemlist){
	uni.setStorage({
	    key: 'itemlist',
	    data: itemlist
	})
	console.info("设置itemlist成功 " + itemlist);
}

function getitemlist(){
	return uni.getStorageSync("itemlist");
}

function goBack(pageNum) {
    uni.navigateBack({
        delta: pageNum
    });
}

// 解决回退页面toast消失问题
function toastAndBack(content) {
    uni.showToast({
        title: content,
        icon: 'none'
    });
    setTimeout(() => {
        uni.navigateBack({
            delta: 1
        });
    }, 2000)
}

function setPassword(password) {
    uni.setStorage({
        key: 'currentPassword',
        data: password
    })
    console.info("设置password成功 " + password);
}

function setName(name) {
    uni.setStorage({
        key: 'userName',
        data: name
    })

}

function setOrgCode(orgCode) {
    uni.setStorage({
        key: 'orgCode',
        data: orgCode
    })
}

function getOrgCode() {
    return uni.getStorageSync("orgCode");
}

function setOrgName(orgName) {
    uni.setStorage({
        key: 'orgName',
        data: orgName
    })
}

function getOrgName() {
    return uni.getStorageSync("orgName");
}

function setVersion(versionCode) {
    uni.setStorage({
        key: 'versionCode',
        data: versionCode
    })

}

function getversionCode() {
    return uni.getStorageSync("versionCode");
}

function getPassword() {
    return uni.getStorageSync("currentPassword");
}

function getuserName() {
    return uni.getStorageSync("userName");
}
/**
 * 处理空的参数
 * @param datas
 * @returns
 */
function cleanParams(datas) {
    var v_data = {};
    for (var a in datas) {
        if (datas[a] != null && datas[a] instanceof Array) {
            v_data[a] = [];
        } else {
            v_data[a] = null;
        }
    }
    return v_data;
};

function isNull(input) {
    if (input == undefined ||
        input == null ||
        input == '') {
        return true;
    } else {
        return false;
    }
};

function getPageUrl(input) {
    return "/" + input + "/" + input;
};

function getToken() {
    return uni.getStorageSync("currentToken");
}

function setToken(token) {
    uni.setStorage({
        key: 'currentToken',
        data: token
    })
    console.info("设置token成功 " + token);
}

function getAccount() {
    return uni.getStorageSync("currentAccount");
}

function setAccount(account) {
    uni.setStorage({
        key: 'currentAccount',
        data: account
    })
    console.info("设置account成功 " + account);
}

function getCurrentDate() {
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth(); //得到月份
    var date = now.getDate(); //得到日期
    var day = now.getDay(); //得到周几
    var hour = now.getHours(); //得到小时
    var minu = now.getMinutes(); //得到分钟
    var sec = now.getSeconds(); //得到秒
    // var MS = now.getMilliseconds(); //获取毫秒
    var week;
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    week = arr_week[day];
    var time = "";
    // time = year + "年" + month + "月" + date + "日" + " " + hour + ":" + minu + ":" + sec + " " + week;
    time = year + "/" + month + "/" + date + " " + hour + ":" + minu;
    return time;
}

// 数组按指定字段去重
function unique(arr, attr) {
    const res = new Map();
    return arr.filter((item) => {
        var attrItem = item[attr]
        return !res.has(attrItem) && res.set(attrItem, 1)
    })
}

function requestRest(url, method, command) {
    console.info("请求接口[" + url + "]");
    let result = uni.request({
        url: url,
        method: method,
        data: command,
        header: {
            "token": getToken()
        }
    })
    // console.log('服务器返回值 ', JSON.stringify(result))
    return result;
}

function requestRestgetnoparam(url, method) {
    console.info("请求接口[" + url + "]");
    let result = uni.request({
        url: url,
        method: method,
        header: {
            "token": getToken()
        }
    })
    return result;
}

function requestRestget(url, method, command) {
    console.info("请求接口[" + url + "]");
    let result = uni.request({
        url: url,
        method: method,
        data: command,
        header: {
            "token": getToken()
        }
    })
    console.log('服务器返回值 ', JSON.stringify(result))
    return result;
}

function preventRepeat(e = {}) {
    if (!preventRepeatEvent[e.key]) {
        console.log("按键成功[" + e.key + "]");
        preventRepeatEvent[e.key] = true
        console.log(JSON.stringify(e.success()));
        e.success && e.success();
        if (e.time) {
            setTimeout(() => {
                console.log("按键可用了[" + e.key + "]");
                delete preventRepeatEvent[e.key]
            }, e.time)
        }
    } else {
        console.log("本次为重复按键无效[" + e.key + "]");
        e.fail && e.fail()
    }
}
